using MediatR;
using RBAC.Domain.WarehouseManagement;
using RBAC.ErrorCode;
using RBAC.Interstructrue.Base;
using RBAC.Read.Api.Application.Command.ERP.WarehouseManagement;
using Dapper;
using System.Linq;
namespace RBAC.Read.Api.Application.Handler.ERP.WarehouseManagement
{
    public class GetprintWarehouseCommandHandler : IRequestHandler<GetprintWarehouseCommands, APIResult<List<WarehouseList>>>
    {
        private readonly IBaseRepository<WarehouseList> _warehouseRepo;

        public GetprintWarehouseCommandHandler(IBaseRepository<WarehouseList> warehouseRepo)
        {
            _warehouseRepo = warehouseRepo;
        }

        public Task<APIResult<List<WarehouseList>>> Handle(GetprintWarehouseCommands request, CancellationToken cancellationToken)
        {
            try
            {
                var where = string.Empty;
                var param = new DynamicParameters();
                if (!string.IsNullOrWhiteSpace(request.WarehouseName))
                {
                    where = "WarehouseName LIKE @WarehouseName";
                    param.Add("@WarehouseName", $"%{request.WarehouseName}%");
                }

                // 不分页，打印场景直接返回完整列表（可按需要前端分页/打印）
                var data = _warehouseRepo.GetAll(where, param) ?? new List<WarehouseList>();

                return Task.FromResult(new APIResult<List<WarehouseList>>
                {
                    Code = APIEnums.Success,
                    Msg = "查询成功",
                    Data = data
                });
            }
            catch (Exception ex)
            {
                return Task.FromResult(new APIResult<List<WarehouseList>>
                {
                    Code = APIEnums.Fail,
                    Msg = "查询失败：" + ex.Message,
                    Data = new List<WarehouseList>()
                });
            }
        }
    }
}
